package com.shopnight.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shopnight.entity.ChatRoomInferior;
import com.shopnight.mapper.UserMapper;
import com.shopnight.service.ChatRoomInferiorService;
import com.shopnight.mapper.ChatRoomInferiorMapper;
import com.shopnight.socketVo.MessageVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Map;

/**
* @author 13201
* @description 针对表【chat_room_inferior】的数据库操作Service实现
* @createDate 2023-05-12 13:37:10
*/
@Service
public class ChatRoomInferiorServiceImpl extends ServiceImpl<ChatRoomInferiorMapper, ChatRoomInferior>
    implements ChatRoomInferiorService{

    @Resource
    private ChatRoomInferiorMapper chatRoomInferiorMapper;
    @Autowired
    private UserMapper userMapper;
    @Override
    public Page pageMessage(Map<String, String> params) {
        Page page = new Page<>(Long.parseLong(params.get("page")),Long.valueOf(params.get("pageSize")) );
        Page<MessageVo> resPage = chatRoomInferiorMapper.pageMessage(page, params);

        resPage.getRecords().parallelStream().forEach( messageVo -> {
          if (messageVo.getFatherId() != null ){
              ChatRoomInferior chatRoomInferior = chatRoomInferiorMapper.selectById(messageVo.getFatherId());
              MessageVo.fatherMessageVo fatherMessageVo = new MessageVo.fatherMessageVo();
              BeanUtils.copyProperties(chatRoomInferior,fatherMessageVo);
              fatherMessageVo.setUserNick( userMapper.selectById(fatherMessageVo.getUserId()).getUserNick() );
              messageVo.setFatherMessageVo(fatherMessageVo);
          }
        });

        return resPage;
    }

}




